package com.example.jf.jfapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.jf.jfapp.ArticleActivity;
import com.example.jf.jfapp.R;
import com.example.jf.jfapp.bean.ImformationBean;
import com.example.jf.jfapp.bean.response.ImformationResponse;
import com.example.jf.jfapp.widget.GlideCircleTransform;

import java.io.IOException;

/**
 * 位置：
 * 作用：
 * 时间：2017/7/27
 */

public class ZixunaDapter extends RecyclerView.Adapter<ZixunaDapter.ItemViewHolder> {

    private ImformationResponse mImformationResponse;
    private Context mContext;

    public ZixunaDapter(Context mContext, ImformationResponse mImformationResponse) {
        this.mImformationResponse = mImformationResponse;
        this.mContext = mContext;
    }

    @Override
    public ZixunaDapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_imformation, parent, false);
        ItemViewHolder itemViewHolder = new ItemViewHolder(view);
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(final ZixunaDapter.ItemViewHolder holder, final int position) {
        final ImformationResponse.ResultBean bean = mImformationResponse.getResult().get(position);
        try {
            if (bean.getAuthor().getUsername() == null || ("").equals(bean.getAuthor().getUsername())) {
            } else {
                holder.tv_zixun_username.setText(bean.getAuthor().getUsername());

            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        try {
            Glide.with(holder.itemView.getContext())
                    .load(bean.getAuthor().getHeadProtrait())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .transform(new GlideCircleTransform(holder.itemView.getContext()))
                    .error(R.mipmap.ic_launcher)
                    .into(holder.im_user_head);
        } catch (NullPointerException e) {
            holder.im_user_head.setImageResource(R.mipmap.ic_launcher);
        }

        holder.tv_zixun_timestamp.setText(bean.getTimestamp() + " ");
        holder.tv_zixun_content.setText(bean.getSynopsis());

        Glide.with(mContext)
                .load(bean.getCoverImage())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade()
                .error(R.mipmap.ic_launcher)
                .centerCrop()
                .into(holder.im_zixun_coverImage);
        holder.im_zixun_coverImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentArticle=new Intent(mContext, ArticleActivity.class);
                intentArticle.putExtra("getcontent",bean.getContent());
                mContext.startActivity(intentArticle);
            }
        });

        holder.tv_zixun_commentSize.setText(bean.getCommentsSize() + "");
        holder.tv_zixun_likeSize.setText(bean.getLikeSize() + "");

    }

    @Override
    public int getItemCount() {


            return mImformationResponse.getResult().size();

    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView im_user_head;
        TextView tv_zixun_username;
        TextView tv_zixun_content;
        TextView tv_zixun_timestamp;
        ImageView im_zixun_coverImage;
        TextView tv_zixun_commentSize;
        TextView tv_zixun_likeSize;

        public ItemViewHolder(View itemView) {
            super(itemView);
            im_user_head = itemView.findViewById(R.id.im_user_head);
            tv_zixun_username = itemView.findViewById(R.id.tv_zixun_username);
            tv_zixun_timestamp = itemView.findViewById(R.id.tv_zixun_timestamp);
            im_zixun_coverImage = itemView.findViewById(R.id.im_zixun_coverImage);
            tv_zixun_commentSize = itemView.findViewById(R.id.tv_zixun_commentSize);
            tv_zixun_likeSize = itemView.findViewById(R.id.tv_zixun_likeSize);
            tv_zixun_content = itemView.findViewById(R.id.tv_zixun_content);
        }
    }

}
